<?php
/**
* Quản trị người dùng
*/
class Admin_UsersController extends Zend_Controller_Action{
		
	function init(){
            System_Template::using('blank','Admin');
            $this->Params = $this->getRequest()->getParams();            
            $this->view->maintitle = "Quản lý người dùng";
            $this->model = new Admin_Model_Users();
            parent::init();
	}	
	function indexAction(){           
            $this->view->maintitle = "Danh sách người dùng";
            $model = new Admin_Model_Users();           
            $NAME = $this->_request->getParam("USERNAME");
            $phamvi = $this->_request->getParam("phamvi");
            $this->view->USERNAME = $NAME;
            $this->view->phamvi = $phamvi;
            
            $parameter = array('USERNAME'=>$NAME,'phamvi'=>$phamvi);
            $count = $model->count($parameter);
            
            $paging = common::pagingAuto($count);
            $data = $model->SelectAll($parameter,$paging[0],$paging[1]);
            
            $this->view->data = $data;

            common::showButton('add');
            common::showButton('delete');
	}
    public function inputAction(){
        common::showButton('save');
        common::showButton('back');
        $tableRole = new System_Table_Roles();
        $tableCompany = new System_Table_Company();
        $this->qluser = new Admin_Model_Users();
        $listRole = $tableRole->selectAll();
        
        $this->view->maintitle = "Thêm mới người dùng";
        $id= (isset($this->Params['id'])?$this->Params['id']:'');
        
        /* Trường hợp cập nhật */
        if($id>0){            
            $data=$this->qluser->find($id)->current();
            $this->view->data=$data;
            $this->view->id_u = $id;
        }
        $listCompany = $tableCompany->fetchAll();
        $this->view->listCompany = $listCompany ;
        
        $this->view->listRole = $listRole;
    }
    public function activeAction() {
        $db = Zend_Db_Table::getDefaultAdapter();
        $active= 'approved';
        $id_u = $this->Params['id_u'];
        $data = array('STATUS' => $active);
        $rs = $db->update("ha_users",$data, "ID_U=" . (int) $id_u);
        ajax::ship("res", $rs);
        ajax::ship("active", $active);
        ajax::ship("id_u", $id_u);
        exit;
    }
    public function saveAction(){
        $isValid = $this->model->GetUserByUser($this->Params['USERNAME']);
        $this->model->AddNewUser($this->Params);
        $this->_redirect('/admin/users/index');
    }
    function deleteAction() {
        $params = $this->getRequest()->getParams();
        $db = Zend_Db_Table::getDefaultAdapter();
        $ids = $params["DEL"];
        foreach($ids as $t){
            $db->delete("ha_users","ID_U=".(int)$t);
        }
        
        $this->_redirect("/admin/users/index");
    }
}